package com.txy.trigger.http;

import com.txy.api.IRaffleService;
import com.txy.api.dto.RaffleAwardListRequestDTO;
import com.txy.api.dto.RaffleAwardListResponseDTO;
import com.txy.api.dto.RaffleRequestDTO;
import com.txy.api.dto.RaffleResponseDTO;
import com.txy.types.common.Response;
import com.txy.domain.strategy.model.entity.RaffleAwardEntity;
import com.txy.domain.strategy.model.entity.RaffleFactorEntity;
import com.txy.domain.strategy.model.entity.StrategyAwardEntity;
import com.txy.domain.strategy.service.IRaffleAward;
import com.txy.domain.strategy.service.armory.IStrategyArmory;
import com.txy.types.enums.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/api/${app.config.app-version}/raffle")
@CrossOrigin("${app.config.cross-origin}")
public class RaffleController implements IRaffleService {

    @Resource
    private IStrategyArmory strategyArmory;

    @Resource
    private IRaffleAward raffleAward;

    @Resource
    private com.txy.domain.strategy.service.IRaffleService raffleService;

    @Override
    @RequestMapping(value = "strategy_armory", method = RequestMethod.GET)
    public Response<Boolean> strategyArmory(@RequestParam Long strategyId) {
        try {
            log.info("抽奖装配，strategyArmory strategyId:{}", strategyId);
            boolean armoryRes = strategyArmory.assembleLotteryStrategy(strategyId);
            log.info("装配结束...");
            return Response.<Boolean>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(armoryRes).build();
        } catch (Exception e) {
            log.error("装配失败", e);
            return Response.<Boolean>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .data(false).build();
        }
    }

    @Override
    @RequestMapping(value = "query_raffle_award_list", method = RequestMethod.POST)
    public Response<List<RaffleAwardListResponseDTO>> queryRaffleAwardList(@RequestBody RaffleAwardListRequestDTO raffleAwardListRequestDTO) {
        try {
            List<StrategyAwardEntity> strategyAwardEntities = raffleAward.queryRaffleAwardList(raffleAwardListRequestDTO.getStrategyId());
            List<RaffleAwardListResponseDTO> raffleAwardListResponseDTOS = new ArrayList<>();
            for (StrategyAwardEntity strategyAwardEntity : strategyAwardEntities) {
                RaffleAwardListResponseDTO responseDTO = RaffleAwardListResponseDTO.builder()
                        .awardId(strategyAwardEntity.getAwardId())
                        .awardTitle(strategyAwardEntity.getAwardTitle())
                        .awardSubtitle(strategyAwardEntity.getAwardSubtitle())
                        .sort(strategyAwardEntity.getSort())
                        .build();
                raffleAwardListResponseDTOS.add(responseDTO);
            }

            return Response.<List<RaffleAwardListResponseDTO>>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(raffleAwardListResponseDTOS).build();

        } catch (Exception e) {
            log.error("queryRaffleAwardList 失败", e);
            return Response.<List<RaffleAwardListResponseDTO>>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .data(null).build();
        }
    }

    @Override
    @RequestMapping(value = "random_raffle", method = RequestMethod.POST)
    public Response<RaffleResponseDTO> randomRaffle(@RequestBody RaffleRequestDTO requestDTO) {
        try {
            RaffleAwardEntity raffleAwardEntity = raffleService.performRaffle(RaffleFactorEntity.builder()
                    .strategyId(requestDTO.getStrategyId())
                    .userId("101").build());
            RaffleResponseDTO responseDTO = RaffleResponseDTO.builder()
                    .awardId(raffleAwardEntity.getAwardId())
                    .awardIndex(raffleAwardEntity.getSort())
                    .awardTitle(raffleAwardEntity.getAwardTitle())
                    .build();

            return Response.<RaffleResponseDTO>builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .info(ResponseCode.SUCCESS.getInfo())
                    .data(responseDTO).build();
        } catch (Exception e) {
            log.error("randomRaffle 失败", e);
            return Response.<RaffleResponseDTO>builder()
                    .code(ResponseCode.UN_ERROR.getCode())
                    .info(ResponseCode.UN_ERROR.getInfo())
                    .data(null).build();
        }
    }
}
